import java.sql.*;
import javax.sql.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;

public class CsvUploader {

	private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
	private static final String DB_URL = "jdbc:mysql://localhost/Miner";
	//"jdbc:mysql://songrecommender.csmwbjcoq6lu.us-east-1.rds.amazonaws.com/song_recommendation";
	
	static final String USER = "aws";
	static final String PASS = "guhaminer";	
	
	public static void main(String[] args) {
		
		Connection conn = null;
		Statement stmt = null;
		
		try
		{
			Class.forName(JDBC_DRIVER);
			
			System.out.println("Connecting to database...");
			conn = DriverManager.getConnection(DB_URL, USER, PASS);
			
			stmt = conn.createStatement();
			
			
			BufferedReader fileBuffer = new BufferedReader(new FileReader(args[0]));
			//first is metadata
			String fileLine = fileBuffer.readLine();
			fileLine = fileBuffer.readLine();
			while ( fileLine != null)
			{
				String[] songData = fileLine.split(",");
				if (songData.length != 12)
				{
					//end of file
					break;
				}

				String sql = String.format(
						"INSERT into `Miner`.`Song` VALUES ('%s',0, '%s', '%s', '%s','%s', '%s', " +
															"'%s', '%s','%s', '%s','%s', '%s');",
															songData[0], songData[1],songData[2], songData[3],
															songData[4], songData[5],songData[6], songData[7],
															songData[8], songData[9], songData[10], songData[11]);
				
				try
				    {
					stmt.executeUpdate(sql);
				    }
				catch (Exception e)
				    {
					e.printStackTrace();
					continue;
				    }
				fileLine = fileBuffer.readLine();

			}
			
			fileBuffer.close();
			
			conn.close();
			stmt.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

}
